/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author alocubiche
 */
public class ItemModel {
    
    public static ArrayList<Item> selectAll()
    {
        ArrayList<Item> lesArticles = new ArrayList<Item>();
        if (MySQLManager.connect())
        {
            try {
                    Connection conn = MySQLManager.getConnection();
                    Statement s = conn.createStatement ( );
                    s.executeQuery ("SELECT id, name, price,available FROM item WHERE available > 0");
                    ResultSet rs = s.getResultSet ( );
                    
                    int i = 0;
                    
                    while (rs.next ()){
                        Item unArticle = new Item();
                        unArticle.setId(Integer.parseInt(rs.getString("id")));
                        unArticle.setQuantity(Integer.parseInt(rs.getString("available")));
                        unArticle.setName(rs.getString("name"));
                        unArticle.setPrice(Float.parseFloat(rs.getString("price")));
                        lesArticles.add(unArticle);
                        i++;
                    }
                    System.out.println(i + " lignes sélectionnées");

                    rs.close (); s.close ( );
                    MySQLManager.disconnect();
            }
            catch (Exception e) {
               e.printStackTrace();
            }
        }
        return lesArticles;
    }
    
    public static Item selectOne(int id, int quantity)
    {
        Item article = new Item();
        if (MySQLManager.connect())
        {
            try {
                    Connection conn = MySQLManager.getConnection();
                    Statement s = conn.createStatement ( );
                    s.executeQuery ("SELECT id, name, price,available FROM `item` WHERE id = " + id + " LIMIT 0,30;");

                    ResultSet rs = s.getResultSet ( );

                    while (rs.next ()){
                        article.setId(Integer.parseInt(rs.getString("id")));
                        article.setName(rs.getString("name"));
                        article.setQuantity(quantity);
                        article.setPrice(Float.parseFloat(rs.getString("price")));
                    }
                    rs.close (); s.close ( );
                    MySQLManager.disconnect();
            }
            catch (Exception e) {
               e.printStackTrace();
            }
        }
        return article;
    }
    
    public static void InsertOne(String UnNom, int UnPrix, int UneQuantite){
        int rows = -1;
        if (MySQLManager.connect())
        {
            try {
                    Connection conn = MySQLManager.getConnection();
                    PreparedStatement pstmt = null;
                    pstmt = conn.prepareStatement("INSERT INTO `item` (name,price,available) VALUES ('" + UnNom + "'," + UnPrix + "," + UneQuantite + ");");
                    rows = pstmt.executeUpdate();
                    MySQLManager.disconnect();
            }
            catch (Exception e) {
               e.printStackTrace();
            }
            System.out.println(rows + " ligne insérée");
        }
        
    }
    
}

